package fire.and.near.challenger.fragments;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import fire.and.near.challenger.R;
import fire.and.near.challenger.database.ChallengeModel;
import fire.and.near.challenger.database.ChallengeModel.ChallengeType;
import fire.and.near.challenger.database.ChallengeStatus;
import fire.and.near.challenger.util.Consts;

public class ChallengesListAdapter extends ArrayAdapter<ChallengeModel> {

	private List<ChallengeModel> challenges = null;

	public ChallengesListAdapter(Context context, int resource) {
		super(context, resource);
		// TODO Auto-generated constructor stub
	}

	public ChallengesListAdapter(Context context, int resource,
			List<ChallengeModel> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertedView, ViewGroup parent) {
		View v = convertedView;

		if (v == null) {
			LayoutInflater inflater = ((Activity) this.getContext())
					.getLayoutInflater();
			v = inflater.inflate(R.layout.challenges_list_row, null);
		}

		ChallengeModel challenge = getItem(position);

		if (challenge != null) {
			TextView tvDesc = ((TextView) v
					.findViewById(R.id.textView_description_list));

			tvDesc.setText(challenge.getDescription() == null ? challenge
					.getAnswer() : challenge.getDescription());

			String frName = challenge.getChallenger().getFbID() == challenge.getChallengedID() ?
					Consts.ME : (challenge.getChallenger().getName() == null ? Consts.COMPANY_NAME :
						challenge.getChallenger().getName());
			
			((TextView) v.findViewById(R.id.textView_friend_name_list))
					.setText(frName);

			if (challenge.getStatus() == ChallengeStatus.IN_HISTORY) {
				if (challenge.isAccepted()) {

					if (challenge.getChallengeType() == ChallengeType.TEXT) {
						setTextboxIcon(tvDesc, v, R.drawable.text_success);
					} else {
						setTextboxIcon(tvDesc, v, R.drawable.photo_success);
					}
				} else {
					if (challenge.getChallengeType() == ChallengeType.TEXT) {
						setTextboxIcon(tvDesc, v, R.drawable.text_fail);
					} else {
						setTextboxIcon(tvDesc, v, R.drawable.photo_fail);
					}
				}
			} else {
				if (challenge.getChallengeType() == ChallengeType.PHOTO) {
					setTextboxIcon(tvDesc, v, R.drawable.photo);
				} else {
					setTextboxIcon(tvDesc, v, R.drawable.text);
				}
			}
		}

		return v;
	}

	private void setTextboxIcon(TextView target, View parent, int resId) {
		target.setCompoundDrawablesWithIntrinsicBounds(parent.getResources()
				.getDrawable(resId), null, null, null);
	}

	public List<ChallengeModel> getChallenges() {
		return challenges;
	}

	public void setChallenges(List<ChallengeModel> challenges) {
		this.challenges = challenges;
	}
}
